<template>
  <div>
    <el-table :data="data" style="width: 100%" border v-loading="drafloading">
      <el-table-column prop="projectCode" label="项目编号" width="200">
      </el-table-column>
      <el-table-column
        prop="projectName"
        label="项目名称"
        width="200"
      ></el-table-column>
      <el-table-column prop="projectPerson" label="项目负责人" width="200">
      </el-table-column>
      <el-table-column
        prop="startTime"
        label="项目开始时间"
        width="200"
      ></el-table-column>
      <el-table-column
        prop="endTime"
        label="项目结束时间"
        width="200"
      ></el-table-column>
      <el-table-column prop="price" label="项目金额" width="200">
        <template slot-scope="scope">
          <div>{{ formatCurrency(scope.row.price, 2) }}</div>
        </template>
      </el-table-column>
      <el-table-column prop="lookMember" label="项目成员" width="200">
        <template slot-scope="scope">
          <el-button size="small" type="primary" @click="lookMember(scope.row)"
            >查看成员</el-button
          >
        </template>
      </el-table-column>
      <el-table-column prop="remainDate" label="项目所剩时间" width="200">
        <template slot-scope="scope">
          <div :style="{ color: dateTotal > 3 ? 'green' : 'red' }">
            <div>{{ scope.row.remainDate }}天</div>
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="projectStatus" label="项目状态" width="200">
        <template slot-scope="scope">
          <div v-if="scope.row.status == '0'">
            <el-tag>草稿中</el-tag>
          </div>
          <div v-else-if="scope.row.status == '1'">
            <el-tag type="warning">已创建</el-tag>
          </div>
        </template>
      </el-table-column>
      <el-table-column :fixed="'right'" prop="control" label="操作" width="300">
        <template slot-scope="{ row }">
          <el-button
            type="danger"
            size="small"
            @click="deletes(row.projectCode)"
            >删除</el-button
          >
          <el-button type="warning" size="small" @click="createProject(row)"
            >创建项目</el-button
          >
          <el-button type="warning" size="small" @click="editDraf(row)"
            >编辑</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <look :code="code" :show="show" @close="close"></look>
    <!-- 编辑弹窗 -->
    <addDraf
      ref="addDraf"
      :drafVisible="drafVisible"
      :title="title"
      :list="list"
      @drafClose="drafClose"
    />
  </div>
</template>

<script>
import look from "./lookmember.vue";
import addDraf from "./addDraf.vue";
import { url } from "@/utils/url";
export default {
  props: ["data", "drafloading"],
  data() {
    return {
      code: "",
      show: false,
      dateTotal: "",
      drafVisible: false,
      title: "",
      list: [], //选中的
    };
  },
  components: {
    look,
    addDraf,
  },
  watch: {},
  methods: {
    //转化两位小数
    formatCurrency(amount, decimalPlaces) {
      // 确保金额是数字
      const number = Number(amount);
      // 使用toFixed()方法确定小数位数
      const formatted = number.toFixed(decimalPlaces);
      // 返回格式化后的金额字符串
      return formatted;
    },
    lookMember(e) {
      this.show = true;
      this.code = e.projectCode;
    },
    //关闭
    close(e) {
      if (e == true) {
        this.show = false;
        this.code = "";
      }
    },
    //删除
    deletes(e) {
      let obj = {
        projectCode: e,
      };
      this.$http.get("/deleteDrafProject", { params: obj }).then((res) => {
        if (res.code == 200) {
          this.$message.success("删除成功");
          this.$emit("drafRefresh", true);
          return;
        }
        this.$message.error(res.msg);
      });
    },
    //创建项目
    createProject(row) {
      this.$http.post("/drafChangeToList", row).then((res) => {
        if (res.code == 200) {
          this.$message.success("创建成功");
          this.$emit("drafRefresh", true);
          return;
        }
        this.$message.error(res.msg);
      });
    },
    //编辑草稿
    editDraf(row) {
      console.log(row);
      this.title = "草稿-编辑";
      this.drafVisible = true;
      this.$refs.addDraf.form = Object.assign(this.$refs.addDraf.form, row);
      //文件列表
      this.$refs.addDraf.fileList=this.takFile(row)
      //成员
      this.list = this.splitMethod(row.member);
    },
    //关闭草稿
    drafClose(e) {
      if (e == true) {
        this.drafVisible = false;
        this.$emit("drafRefresh", true);
      }
    },
    //拆分
    splitMethod(e) {
      console.log(e, "eeee");
      let arrs = [];
      if (e.indexOf("/") != -1) {
        arrs = e.split("/");
      } else {
        arrs.push(e);
      }
      return arrs;
    },
    //文件列表回显
    takFile(objs) {
      let arr = [];
      if (objs.projectURL != "") {
        let obj = {
          name: objs.projectURL,
          url: url + objs.projectURL,
        };
        arr.push(obj);
      }

      return arr;
    },
  },
};
</script>

<style>
</style>